package GUI;

import java.awt.Color;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import Client_logic.DealSale;
import Client_logic.ClientNotInitializesException;
import Client_logic.ConnectionToServer;
import Client_logic.Customer;

/**
 * This the main panel for SellsMen, all turn to client start from this class. 
 * @author michael
 * @author michael
 * @author michael
 * @author michael
 */
public class SalesManForm extends DealSaleForm{
	private JLabel cIdLabel = null;
	private JLabel cNameLabel= null;
	private JLabel cAgeLabel= null;
	private JLabel cAreaLabel= null;
	private JLabel cPhoneLabel= null;
	private JLabel cImportance = null;
	private JTextField cIdTextField = null;
	private JTextField cNameTextField = null;
	private JTextField cAgeTextField = null;
	private JComboBox cAreaJComboBox= null;
	private JTextField cPhoneTextField= null;
	private JTextField cImportanceTextField= null;
	private JComboBox clientJComboBox= null;
	private JLabel clientsLabel = null;
	private String username = null;
	private JButton delayButton = null;
	private JButton purchases = null;
	private Customer customer = null;
	private JTextField delayTextField= null;
	private JLabel delayLabel = null;
	private DealSale dealsale = null;
	private boolean delayflag;
	
	
	/**
	 * This is the default constructor
	 */
	public SalesManForm(String username) {
		super();
		this.username = username;
		initialize();		
	}
	
	/**
	 * This method initializes CustomerForm
	 */
	private void initialize() {
		customer = new Customer(0, "", 0,  0, "");
		dealsale = new DealSale(0, null, null, null,null); 
		delayflag= false;
		
		cIdLabel = new JLabel();
		cIdLabel.setBounds(new Rectangle(15, 320, 130, 22));
		cIdLabel.setText("Customer ID:");
		
		cNameLabel = new JLabel();
		cNameLabel.setBounds(new Rectangle(15, 350, 130, 22));
		cNameLabel.setText("Customer name:");
		
		cAgeLabel = new JLabel();
		cAgeLabel.setBounds(new Rectangle(15, 380, 130, 22));
		cAgeLabel.setText("Customer age:");
		
		cAreaLabel = new JLabel();
		cAreaLabel.setBounds(new Rectangle(15, 410, 130, 22));
		cAreaLabel.setText("Customer area:");
		
		cPhoneLabel = new JLabel();
		cPhoneLabel.setBounds(new Rectangle(15,440,130,22));
		cPhoneLabel.setText("Customer phone");
		
		cImportance = new JLabel();
		cImportance.setBounds(new Rectangle(15,470,130,22));
		cImportance.setText("Customer importance");
		
		clientsLabel = new JLabel();
		clientsLabel.setBounds(new Rectangle(350, 300, 127, 25));
		clientsLabel.setText("All customer:");
		
		delayLabel = new JLabel();
		delayLabel.setBounds(new Rectangle(350, 370, 127, 25));
		delayLabel.setText("Delay message:");
		
		this.add(clientsLabel, null);
		
		this.add(delayLabel, null);
		this.add(cIdLabel, null);
		this.add(cNameLabel, null);
		this.add(cAgeLabel, null);
		this.add(cAreaLabel, null);
		this.add(cPhoneLabel,null);
		this.add(cImportance,null);
		this.add(getDelayTextField(), null);
		this.add(getCIdTextField(), null);
		this.add(getCNameTextField(), null);
		this.add(getCAageTextField(), null);
		this.add(getCAreaJComboBox(), null);
		this.add(getCPhoneTextField(),null);
		this.add(getCImportanceTextField(),null);
		this.add(getClientJComboBox(), null);
		this.add(getDelayButton(), null);
		this.add(getPurchases(), null);		
		
		super.getBackjButton().setBounds(140, 510, 90, 25);	
		
		this.setSize(550, 550);
	}
	
	/**
	 * This method initializes delayButton
	 */	
	public JButton getDelayButton(){
		if (delayButton == null){
			delayButton = new JButton();
			delayButton.setText("Delay");
			delayButton.setBounds(new Rectangle(250, 510, 90, 29));
			delayButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (delayflag){
						String msg = getDelayTextField().getText();
						if (msg.equals("")){
							JOptionPane.showMessageDialog(null, "PLZ enter customer message","",JOptionPane.WARNING_MESSAGE);
							return;
						}						
						delayTextField.setEditable(false);
						getPurchases().setEnabled(true);
						delayLabel.setForeground(Color.black);
						delayflag = false;
						long cID = customer.getCustID();
						long activeID = dealsale.getDealSaleID();
						ConnectionToServer connectionToServer;
						try {
							connectionToServer = new ConnectionToServer();
							String serversmsg = (String)connectionToServer.delayTemplate(cID,activeID, msg);
							if (serversmsg != null){
								if (serversmsg.equals("OK")){
									JOptionPane.showMessageDialog(null, "Delay successfuly saved");
									if (clientJComboBox.getItemCount() == 1)
										System.exit(0);
									clientJComboBox.removeItem(cID);
								}
								else
									JOptionPane.showMessageDialog(null, "Delay not saved","", JOptionPane.WARNING_MESSAGE);
							}	
						} 
						catch (Exception e1){
							JOptionPane.showMessageDialog(null, "ERROR!!","", JOptionPane.ERROR_MESSAGE);
						}					
					}
					else{
						getPurchases().setEnabled(false);
						delayTextField.setEditable(true);
						delayLabel.setForeground(Color.red);						
						delayflag = true;
					}					
				}
			});	
		}	
		return delayButton;				
	}
	
	/**
	 * This method initializes delayButton
	 */	
	public JButton getPurchases(){
		if (purchases == null){
			purchases = new JButton();
			purchases.setText("Buy");
			purchases.setBounds(new Rectangle(360, 510, 90, 29));
			purchases.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					long cID = customer.getCustID();
					long activeID = dealsale.getDealSaleID();
					ConnectionToServer connectionToServer;
					try {
						connectionToServer = new ConnectionToServer();
						String serversmsg = (String)connectionToServer.purchasesCampaign(cID,activeID);
						if (serversmsg != null){
							if (serversmsg.equals("OK")){
								JOptionPane.showMessageDialog(null, "Purchase successfuly saved");
								if (clientJComboBox.getItemCount() == 1)
									System.exit(0);
								clientJComboBox.removeItem(cID);
							}
							else
								JOptionPane.showMessageDialog(null, "Purchase not saved","", JOptionPane.WARNING_MESSAGE);
						}	
					} 
					catch (Exception e1){
						JOptionPane.showMessageDialog(null, "ERROR!!","", JOptionPane.ERROR_MESSAGE);
					}									
				}
			});	
		}	
		return purchases;				
	}
	
	/**
	 * This method initializes cIdTextField	
	 */
	private JTextField getCIdTextField(){
		if (cIdTextField == null) {
			cIdTextField = new JTextField();
			cIdTextField.setBounds(new Rectangle(150, 320, 127, 25));
			cIdTextField.setEditable(false);
		}
		return cIdTextField;
	}
	
	/**
	 * This method initializes cIdTextField	
	 */
	private JTextField getDelayTextField(){
		if (delayTextField == null) {
			delayTextField = new JTextField();
			delayTextField.setBounds(new Rectangle(350, 400, 127, 25));
			delayTextField.setEditable(false);
		}
		return delayTextField;
	}
	
	/**
	 * This method initializes cNameTextField	
	 */
	private JTextField getCNameTextField(){
		if (cNameTextField == null) {
			cNameTextField = new JTextField();
			cNameTextField.setBounds(new Rectangle(150, 350, 127, 25));
			cNameTextField.setEditable(false);
		}
		return cNameTextField;
	}
	
	/**
	 * This method initializes cAgeTextField	
	 */
	private JTextField getCAageTextField(){
		if (cAgeTextField == null) {
			cAgeTextField = new JTextField();
			cAgeTextField.setBounds(new Rectangle(150, 380, 127, 25));
			cAgeTextField.setEditable(false);
		}
		return cAgeTextField;
	}
	
	/**
	 * This method initializes cAreaTextField	
	 */
	private JComboBox getCAreaJComboBox(){
		if (cAreaJComboBox == null) {
			cAreaJComboBox = new JComboBox();
			cAreaJComboBox.setBounds(new Rectangle(150, 410, 127, 25));
			cAreaJComboBox.setEditable(false);
			String[] areaList = null;
			try{
				ConnectionToServer connectiontoserver = new ConnectionToServer();		
				areaList = (String[]) connectiontoserver.loadAllAreas();
				if (areaList == null){
					JOptionPane.showMessageDialog(null, "ERROR!!!");
					cAreaJComboBox.addItem(":");
				}	
				else{
					int len = areaList.length;
					if (len == 0)
						cAreaJComboBox.addItem("0:EMPTY");						
					for(int i=0; i < len; i++){
						cAreaJComboBox.addItem(areaList[i]);
					}	
				}
			}
			catch(Exception e){
				JOptionPane.showMessageDialog(null, "ERROR!!!");
				cAreaJComboBox.addItem(":");
			}
			cAreaJComboBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
				}	
			});	
		}
		return cAreaJComboBox;
	}
	
	/**
	 * This method initializes cPhoneTextField	
	 */
	private JTextField getCPhoneTextField(){
		if (cPhoneTextField == null) {
			cPhoneTextField = new JTextField();
			cPhoneTextField.setBounds(new Rectangle(150, 440, 127, 25));
			cPhoneTextField.setEditable(false);
		}
		return cPhoneTextField;
	}
	
	/**
	 * This method initializes cImportanceTextField	
	 */
	private JTextField getCImportanceTextField(){
		if (cImportanceTextField == null) {
			cImportanceTextField = new JTextField();
			cImportanceTextField.setBounds(new Rectangle(150, 470, 127, 25));
			cImportanceTextField.setEditable(false);	
		}
		return cImportanceTextField;
	}
	
	/**
	 * This method initializes clientJComboBox	
	 */
	private JComboBox getClientJComboBox(){
		if (clientJComboBox == null) {
			clientJComboBox = new JComboBox();
			clientJComboBox.setBounds(new Rectangle(350, 320, 127, 25));
			updateClientJComboBox();
			clientJComboBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						ConnectionToServer connectiontoserver = new ConnectionToServer();
						long cID = (Long)clientJComboBox.getSelectedItem();
						long activeID = (Long)connectiontoserver.loadDealSaleForClient(cID);
						if (cID > 0 && activeID > 0){
							customer.loadCustomer(cID);
							dealsale.loadDealSale(activeID);
							loadForm();
						}						
					} 
					catch (ClientNotInitializesException e1) {
						
					}	
				}	
			});	
		}
		return clientJComboBox;
	}
	
	/**
	 * 
	 */
	public void updateClientJComboBox(){
		long[] cliensList = null;
		try{
			ConnectionToServer connectiontoserver = new ConnectionToServer();		
			cliensList = (long[])connectiontoserver.loadAllClientForSellsmen(username);
			if (cliensList == null){
				JOptionPane.showMessageDialog(null, "ERROR!!!");
				clientJComboBox.addItem(0);
			}	
			else{
				int len = cliensList.length;
				if (len == 0)
					clientJComboBox.addItem(0);						
				for(int i=0; i < len; i++){
					clientJComboBox.addItem(cliensList[i]);
				}	
			}
		}catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "ERROR!!!");
			clientJComboBox.addItem(0);
		}		
	}
	
	/**
	 * This method load client list to form
	 */
	public void loadForm(){
		super.loadDealSale(dealsale);
		cIdTextField.setText(String.valueOf(customer.getCustID()));
		cNameTextField.setText(customer.getCustName()) ;
		cAgeTextField.setText(String.valueOf(customer.getCustAge()));
		cAreaJComboBox.setSelectedItem(customer.getCustAddressZone());
		cPhoneTextField.setText(customer.getCustPhone()) ;
		cImportanceTextField.setText(String.valueOf(customer.getImportance()));	
	}
}
